package org.candan.spider.util;

import java.io.File;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author ZhangShuCheng
 *
 */
public class DateUtil {

	public static SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static SimpleDateFormat dayFormat=new SimpleDateFormat("yyyy-MM-dd");
	public static SimpleDateFormat dayFormat2=new SimpleDateFormat("yyyyMMdd");
	public static DecimalFormat numFmat = new DecimalFormat("#,###,###.###");
	public static long oneDayMsec = 24 * 60 * 60 * 1000;
	
	public static String getNumFmat(double num){
		return numFmat.format(num);
	}
	
	public static Date getTodayZeroDate() {
		try {
			return  DateUtil.dayFormat.parse(DateUtil.dayFormat.format(new Date()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static int getInt(String ints){
		if(ints.matches("^\\d+$")){
			return Integer.parseInt(ints);
		}
		return 0;
		
	}
	
	public static String getTodayZeroDateStr(){
		return  DateUtil.dayFormat.format(new Date());
	}
	
	public static Date getTomorrowZeroDate() {
		Date date = new Date();
		date.setTime(date.getTime() + oneDayMsec);
		try {
			return  DateUtil.dayFormat.parse(DateUtil.dayFormat.format(date));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public static String getTomorrowZeroDateStr() {
		Date date = new Date();
		date.setTime(date.getTime() + oneDayMsec);
		return  DateUtil.dayFormat.format(date);
	}
	
	public static String getDayDateStr(Date date){
		if(date == null){
			return "";
		}
		return dayFormat.format(date);
	}
	
	public static Date getDayDate(String date){
		try {
			return dayFormat.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public static String getDateStr(Date date){
		if(date == null){
			return "";
		}
		return dateFormat.format(date);
	}
	public static Date getDate(String date){
		if(date == null || "".equals(date.trim())){
			return null;
		}
		try {
			return dateFormat.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	public static boolean fileExist(String file){
		return new File(file).exists();
	}
	
}
